using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Net.Sockets;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Globalization;
using System.Net;
using SideShow.Scf.ObjectModel;
using Serialization;
using SideShow;
using System.Drawing;
using System.Collections;
using SideShow.Channel;


namespace TestApp
{
    

    class Program
    {
        static void Main(string[] args)
        {
            //try
            //{


            //Bitmap smallIcon = (Bitmap)Image.FromFile(@"C:\SideShowDebug\591248b9-ad35-47c2-b2fa-2d7c120adc79\0.bmp");
            //Bitmap mediumIcon = (Bitmap)Image.FromFile(@"C:\SideShowDebug\591248b9-ad35-47c2-b2fa-2d7c120adc79\1.bmp");
            //Bitmap largeIcon = (Bitmap)Image.FromFile(@"C:\SideShowDebug\591248b9-ad35-47c2-b2fa-2d7c120adc79\2.bmp");

            //SideShowApplication app = new SideShowApplication(
            //    Guid.NewGuid(),
            //    "TestApp",
            //    CachePolicy.KeepFrequentlyAccessed,
            //    true,
            //    smallIcon,
            //    mediumIcon,
            //    largeIcon,
            //    new Guid[] { SideShowConstants.ScfFormatId });


            //XmlDocument doc = new XmlDocument();
            //doc.Load(@"C:\SideShowDebug\591248b9-ad35-47c2-b2fa-2d7c120adc79\a9a5353f-2d4b-47ce-93ee-759f3a7dda4f\7.xml");
            //SideShow.Scf.Contents.ScfContent c = new SideShow.Scf.Contents.ScfContent(7, doc);
            //app.Endpoints[SideShowConstants.ScfFormatId].AddContent(c);
            //Message msg = new Message(Guid.NewGuid());
            //msg.Args.Add(app);
            //using (MemoryStream ms = new MemoryStream())
            //{
            //    BinarySerializer ser = new BinarySerializer();
            //    ser.Serialize(ms, msg);
            //    ms.Position = 0;
            //    Message c2 = (Message)ser.Deserialize(ms);
            //    ///c2.LargeIcon.Save(@"e:\test.bmp");
            //}
                    
                    
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex);
            //}
            //Console.ReadKey();


            Console.WriteLine("Press any key to connect...");
            Console.ReadKey();
            try
            {

                ClientApp app = new ClientApp();
                ClientApplicationManager appMgr = new ClientApplicationManager(SideShowConstants.ScfFormatId);
                appMgr.ListChanged += delegate(object sender, EventArgs e)
                {
                    WriteMenu(appMgr);
                };
                appMgr.Connect(IPAddress.Loopback, 14000);

                while (true)
                {
                    List<SideShowApplication> apps = new List<SideShowApplication>(appMgr.Applications);
                    WriteMenu(appMgr);
                    ConsoleKeyInfo key = Console.ReadKey();
                    if (key.Key == ConsoleKey.X)
                    {
                        break;
                    }
                    else if (Char.IsNumber(key.KeyChar))
                    {
                        int i = int.Parse(key.KeyChar.ToString());
                        if (i >= 0 && i < apps.Count)
                        {
                            appMgr.CurrentApplication = apps[i];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            //Console.ReadKey();
        }

        private static void WriteMenu(ClientApplicationManager appMgr)
        {
            Console.WriteLine("Current application: ", appMgr.CurrentApplication == null ? "-" : appMgr.CurrentApplication.Name);
            Console.WriteLine();
            for (int i = 0; i < appMgr.Applications.Count; i++)
            {
                Console.WriteLine("{0} - {1}", i, appMgr.Applications[i].Name);
            }
            Console.WriteLine("X - Exit");
            Console.WriteLine();
        }

        
    }
}
